package cn.elead.chaos.remote.web.remote.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.constant.UserConstants;
import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.ApiAssert;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.sso.core.util.SsoUtils;
import cn.elead.chaos.remote.web.remote.domain.SysDept;
import cn.elead.chaos.remote.web.remote.domain.vo.TreeSelect;
import cn.elead.chaos.remote.web.remote.service.ISysDeptService;

/**
 * 部门信息
 * 
 * @author luopeng
 */
@UnifiedReturn
@RestController
@RequestMapping("/system/dept")
public class RemoteDeptController extends BaseController {

	@Autowired
	private ISysDeptService deptService;

	/**
	 * 获取部门列表
	 */
	@GetMapping("/list")
	public List<SysDept> list(SysDept dept) {
		List<SysDept> depts = deptService.selectDeptList(dept);
		return deptService.buildDeptTree(depts);
	}

	/**
	 * 根据部门编号获取详细信息
	 */
	@GetMapping(value = "/{deptId}")
	public SysDept getInfo(@PathVariable Long deptId) {
		return deptService.selectDeptById(deptId);
	}

	/**
	 * 获取部门下拉树列表
	 */
	@GetMapping("/treeselect")
	public List<TreeSelect> treeselect(SysDept dept) {
		List<SysDept> depts = deptService.selectDeptList(dept);
		return deptService.buildDeptTreeSelect(depts);
	}

	/**
	 * 加载对应角色部门列表树
	 */
	@GetMapping(value = "/roleDeptTreeselect/{roleId}")
	public List<Integer> roleDeptTreeselect(@PathVariable("roleId") Long roleId) {
		return deptService.selectDeptListByRoleId(roleId);
	}

	/**
	 * 新增部门
	 */
	@PostMapping
	@Log(title = "部门管理", businessType = BusinessType.INSERT)
	public int add(@Validated @RequestBody SysDept dept) {
		if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在"));
		}
		dept.setCreateBy(SsoUtils.getUserName());
		int insertDept = deptService.insertDept(dept);
		return insertDept;
	}

	/**
	 * 修改部门
	 */
	@PutMapping
	@Log(title = "部门管理", businessType = BusinessType.UPDATE)
	public int edit(@Validated @RequestBody SysDept dept) {
		if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在"));
		} else if (dept.getParentId().equals(dept.getDeptId())) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改部门'" + dept.getDeptName() + "'失败，上级部门不能是自己"));
		}
		dept.setUpdateBy(SsoUtils.getUserName());
		return deptService.updateDept(dept);
	}

	/**
	 * 删除部门
	 */
	@DeleteMapping("/{deptId}")
	@Log(title = "部门管理", businessType = BusinessType.DELETE)
	public int remove(@PathVariable Long deptId) {
		if (deptService.hasChildByDeptId(deptId)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("存在下级部门,不允许删除"));
		}
		if (deptService.checkDeptExistUser(deptId)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("部门存在用户,不允许删除"));
		}
		return deptService.deleteDeptById(deptId);
	}
}
